/*
 * @Author: your name
 * @Date: 2021-12-03 11:55:04
 * @LastEditTime: 2021-12-22 16:54:22
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \express-app\utils\jwt.js
 */
const jwt = require('jsonwebtoken');
const privateKey = 'gwuiVKSNIOGNSK545LGIOWFN324SBH1FGWEGCXJ370BVDVJbrtrntbr';
/**
 *
 * @param {*} payload
 * @returns token
 */
const createToken = (payload) => {
  payload.startTime = +new Date();
  payload.endTime = 1_000 * 60 * 60 * 24 * 7; //未来7天的时间戳
  return jwt.sign(payload, privateKey); //默认hs256加密方式
};
/**
 * @param {*} token
 * @returns verify result
 */
const verifyToken = (token) => {
  return new Promise((resolve, reject) => {
    jwt.verify(token, privateKey, (err, data) => {
      if (err) reject(err);
      resolve(data);
    });
  });
};
module.exports = {
  createToken,
  verifyToken,
};
